﻿using AutoMapper;
using B.S.Procurement.Domain.Sale;
using B.S.Procurement.Error;
using B.S.Procurement.Read.Api.Application.Command.DetailsCommand;
using B.S.Procurement.Read.Api.DTO;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Read.Api.Application.CommandHander.DetailsCommandHander
{
    public class QueryDetailsCommandHander : IRequestHandler<QueryDetailsCommand, ApiPaging<List<DetailsDto>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public QueryDetailsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public Task<ApiPaging<List<DetailsDto>>> Handle(QueryDetailsCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<DetailsDto>> result = new ApiPaging<List<DetailsDto>>();

            var item = db.Queryable<Details>()
                       .InnerJoin<SaleModel>((a, b) => a.SaleId == b.SaleId)
                       .Where((a, b) => a.Del == false)
                       .Select((a, b) => new DetailsDto
                       {
                           DetailsId = a.DetailsId,
                           DetailsName = a.DetailsName,
                           DetailsNumber = a.DetailsNumber,
                           DetailsCard = a.DetailsCard,
                           Unit = a.Unit,
                           Num = a.Num,
                           TaxPrice = a.TaxPrice,
                           Discount = a.Discount,
                           DiscountPrice = a.DiscountPrice,
                           TaxIncludedPrice = a.TaxIncludedPrice,
                           TaxDiscountPrice = a.TaxDiscountPrice,
                           Bill = a.Bill,
                           Rate = a.Rate,
                           TaxDiscountSum = a.TaxDiscountSum,
                           ItemizedOffers = a.ItemizedOffers,
                           DiscountAfterPrice = a.DiscountAfterPrice,
                           DiscountAfter = a.DiscountAfter,
                           BeforeTaxPrice = a.BeforeTaxPrice,
                           Tax = a.Tax,
                           DiscountAfterSum = a.DiscountAfterSum,
                           DeliveryDate = a.DeliveryDate,
                           Remark = a.Remark,
                           Del = a.Del,



                           SaleId = b.SaleId,
                           DocumentType = b.DocumentType,
                           ClientNumber = b.ClientNumber,
                           AgencyTime = b.AgencyTime,
                           ContractSubject = b.ContractSubject,
                           ContractNumber = b.ContractNumber,
                           StartTime = b.StartTime,
                           EndTime = b.EndTime,
                           UserId = b.UserId,
                           ClientId = b.ClientId,
                           ContractCategory = b.ContractCategory,
                           Salespeople = b.Salespeople,
                           DocumentMakers = b.DocumentMakers,
                           Time = b.Time,
                           ContractSum = b.ContractSum,
                           PreferentialSum = b.PreferentialSum,
                           PreferentialAfterSum = b.PreferentialAfterSum,
                           After = b.After,
                           miscellaneous = b.miscellaneous,
                           Billing = b.Billing,
                           BillingType = b.BillingType,
                           Consignee = b.Consignee,
                           Landline = b.Landline,
                           MobilePhone = b.MobilePhone,
                           FullAddress = b.FullAddress,
                       });

            result.TotalCount = item.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            item = item.OrderByDescending(a => a.DetailsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            result.Code = ApiEnum.success;
            result.Msg = "查询成功";
            result.Data = mapper.Map<List<DetailsDto>>(item.ToList());

            return Task.FromResult(result);
        }
    }
}
